<template>
  <div class="org">
    <el-tree
      class="el-tree-1"
      node-key="id"
      ref="orgTree"
      :data="orgList"
      :props="defaultProps"
      @node-click="handleNodeClick"
    ></el-tree>
    <div class="orgList">
      <div class="list-div">
        <el-row>
          <el-col :lg="4" :md="6">
            <el-button type="success" size="small" plain @click="handleSave('saveModal')">新 增</el-button>
            <el-button type="danger" size="small" plain @click="deleteBatch()">删 除</el-button>
          </el-col>
          <el-col :lg="10" :md="12">
            <el-col :lg="10" :md="10">
              <el-input
                size="small"
                @keyup.enter.native="search"
                placeholder="名称/code"
                v-model="searchFilter.condition"
                clearable
              ></el-input>
            </el-col>
            <el-col :offset="1" :span="2">
              <el-button size="small" type="primary" @click="search" plain>查 询</el-button>
            </el-col>
          </el-col>
        </el-row>

        <br>
        <el-table
          :data="tableData"
          border
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="40" header-align="center"></el-table-column>
          <el-table-column type="index" width="40" header-align="center"></el-table-column>
          <el-table-column sortable prop="name" label="组织名称" align="center" width="150"></el-table-column>
          <el-table-column sortable prop="code" align="center" label="组织code"></el-table-column>
          <el-table-column sortable prop="status" align="center" label="状态">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                :active-value="1"
                :inactive-value="2"
                active-color="#13ce66"
                inactive-color="#C0C0C0"
                @change="changeStatus(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column sortable prop="createTime" align="center" label="创建时间"></el-table-column>
          <el-table-column sortable prop="updateTime" align="center" label="更新时间"></el-table-column>

          <el-table-column fixed="right" label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click.native="handleEdit(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination block">
          <el-pagination
            background
            @current-change="handleCurrentChange"
            :current-page.sync="page"
            :page-size="pageSize"
            layout="total, prev, pager, next"
            :total="total"
          ></el-pagination>
        </div>
      </div>
    </div>
    <div class="saveModal">
      <el-dialog  :title="ModalName" :visible.sync="showSaveModal" @close="cancelR" width="50%">
        <el-form
          :rules="saveModalRules"
          ref="saveModal"
          :model="saveModal"
          label-width="150px"
          size="small"
        >
          <br>
          <el-row>
            <el-col :span="20">
              <el-form-item label="组织名称" prop="name">
                <el-input v-model="saveModal.name"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item label="CODE" prop="code" >
                <el-input v-model="saveModal.code" :disabled="codeInputEnable" ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <br>
          <el-row>
            <el-col :offset="6" :span="20">
              <el-form-item size="large">
                <el-button type="primary" @click="save('saveModal')">{{saveBtnName}}</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  name: "User",
  data() {
    return {
      ModalName:"",
      codeInputEnable: false,
      setRoleUserId: "",
      setOrgUserId: "",
      checkRoleAll: false,
      checkRoleList: [],
      roleList: [],
      isIndeterminate: true,
      showSetRoleModal: false,
      showSetOrgModal: false,
      searchFilter: {
        condition: ""
      },
      saveBtnName: "创建账号",
      showSaveModal: false,
      orgList: [
        {
          id: "",
          name: "组织架构",
          items: []
        }
      ],
      tableData: [],
      page: 1,
      pageSize: 10,
      total: 100,
      orgId: null,
      multipleSelection: [],
      saveModalRules: {
        name: [
          {
            required: true,
            message: "请输入组织名称",
            trigger: "blur"
          }
        ],
        code: [
          {
            required: true,
            message: "请输入组织CODE",
            trigger: "blur"
          }
        ]
      },
      saveModal: {
        id: "",
        name: "name", //组织名称
        code: "", //组织CODE
        parentId: ""
      },
      tmpSaveModal: {
        id: "",
        name: "", //组织名称
        code: "", //组织CODE
        parentId: ""
      },
      defaultProps: {
        children: "items",
        label: "name"
      }
    };
  },
  mounted() {
    this.getOrgList();
    this.handleNodeClick();
    console.log(this.saveModal);
  },
  methods: {
    //关闭弹框右上角
    cancelR(){
      this.$refs.saveModal.clearValidate();
    },
    setOrg() {
      let _this = this;
      let orgId = this.$refs.setOrgTree.getCurrentKey();
      if (orgId == null || orgId == "") {
        this.$message.warning("请选择一项有效数据");
        return;
      }
      this.http
        .post("/api/user/setUserOrg", {
          id: this.setOrgUserId,
          orgId: orgId
        })
        .then(res => {
          var data = res.data;
          if (data.code === 200) {
            this.showSetOrgModal = false;
            _this.handleNodeClick();
            this.$message.success(data.msg);
          } else {
            this.$message.error(data.msg);
          }
        });
    },
    //根据条件查询列表
    search() {
      this.http
        .post("/api/org/query", {
          P_NO: this.page,
          P_SIZE: this.pageSize,
          condition:this.searchFilter.condition
        })
        .then(res => {
          var data = res.data;
          this.tableData = data.rows;
          this.total = data.records;
        });
    },
    //新增事件
    handleSave(formName) {
      // this.saveModal = {
      //   name: "",
      //   code: "",
      // }
      let orgId = this.$refs.orgTree.getCurrentKey();
      if (orgId == null || orgId == "") {
        this.$message.warning(
          "未选择左侧指定归属部门,所新增的数据默认归属根部门"
        );
      }
      this.saveBtnName = "创建组织";
      this.saveModal = this.tmpSaveModal;
      this.ModalName="新建组织信息";
      this.showSaveModal = true;
      this.codeInputEnable = false;
      this.saveModal = {
        name: "",
        code: "",
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleCurrentChange(val) {
      this.page = val;
      this.handleNodeClick();
    },
    handleEdit(row) {
      this.saveModal = row;
      this.codeInputEnable = true;
      this.ModalName="编辑组织信息";
      this.showSaveModal = true;
      this.saveBtnName = "保存组织";
    },
    save(formName) {
      let _this = this;
      this.$refs[formName].validate(valid => {
        if (!valid) {
          console.log("error submit!!");
          return false;
        }else{
          this.saveModal.parentId = this.orgId;
          this.http.post("/api/org/save", this.saveModal).then(res => {
            let data = res.data;
            if (data.code === 200) {
              this.$message({
                showClose: true,
                message: data.msg,
                type: "success"
              });
              _this.showSaveModal = false;

              _this.getOrgList();
              _this.handleNodeClick();

            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });

    },
    //批量删除
    deleteBatch() {
      let selectDataList = this.multipleSelection;
      if (selectDataList.length == 0) {
        this.$message({
          message: "请选中一条项再操作",
          type: "warning"
        });
        return;
      }
      let ids = "";
      for (let i = 0; i < selectDataList.length; i++) {
        if (i != selectDataList.length - 1) {
          ids += selectDataList[i].id + ",";
        } else {
          ids += selectDataList[i].id;
        }
      }
      this.$confirm("此操作将永久删除选中的数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.http
          .post("/api/org/deleteBatchIds", {
            ids: ids
          })
          .then(res => {
            let data = res.data;

            if (data.code === 200) {
              this.$message({
                showClose: true,
                message: data.msg,
                type: "success"
              });
              this.getOrgList();
              this.handleNodeClick();
            } else {
              this.$message.error(data.msg);
            }
          });
      });
    },
    changeStatus(row) {
      debugger;
      this.http
        .post("/api/org/save", {
          id: row.id,
          status: row.status
        })
        .then(res => {
          let data = res.data;
          if (data.code === 200) {
            this.$message({
              showClose: true,
              message: data.msg,
              type: "success"
            });
          }
        });
    },
    getOrgList() {
      var _this = this;
      this.http
        .post("/api/org/orgNodeList", {
          type: "org"
        })
        .then(res => {
          let data = res.data.data;
          _this.orgList[0].items = data;
        })
        .catch(err => {});
    },
    handleNodeClick(data) {
      if (data != null) {
        this.orgId = data.id;
      }
      let qColumn;
      let qValue;
      if (this.orgId != null && this.orgId.length > 0) {
        qColumn = "AND_parent_id_LK";
        qValue = this.orgId;
      }
      this.http
        .post("/api/org/query", {
          P_NO: this.page,
          P_SIZE: this.pageSize,
          Q_COLUMN: qColumn != null ? qColumn : "",
          Q_VALUE: qValue != null ? qValue : "",
          P_SORT:"create_time",
          P_ORDER:"DESC"
        })
        .then(res => {
          var data = res.data;
          this.tableData = data.rows;
          this.total = data.records;
        });
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}

.org {
  height: 100%;

  .pagination {
    float: right;
  }
  .el-tree-1 {
    float: left;
    padding: 0px 10px;
    width: 15%;
    height: 100%;
    border-right: 1px solid #ccc;
    box-sizing: border-box;
  }

  .orgList {
    float: left;
    width: 85%;
    padding: 0 10px;
    box-sizing: border-box;
  }
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
